package com.tnstart.utils
{
	import com.tnstart.beans.ControlButton;
	import com.tnstart.beans.ControlLabel;
	
	import flash.events.Event;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.net.URLRequest;
	
	import spark.components.Label;
	import spark.components.View;
	
	import components.IconButton;

	public class ControlUtil
	{
		
		private static var _sounds : Array = [];
		
		public static function play(name : String, ext:String) : void {
			if (name == null || name == "") {
				return;
			}
			var s : Sound = _sounds[name];
			if (s == null) {
				var url : String ="/assets/" + name + ext; 
				trace(url);  
				s = new Sound(new URLRequest(url));
				_sounds[name] = s;
			}
				s.play(0, 1);
			
		}

		
		public static function makeLabel(view:View, controlLabel:ControlLabel):void
		{
			var label:Label = controlLabel.labelRef;
			if(label == null)
			{
				label = new Label();
			}
			label.text = controlLabel.text;
			label.id = controlLabel.id;
			label.top = controlLabel.top;
			label.left = controlLabel.left;
			label.setStyle('color',controlLabel.color);
			label.setStyle('fontSize',controlLabel.fontSize);
			view.addElement(label);
		}
		
		public static function makeIconControlButton(view:View, controlButton:ControlButton):void
		{
			var button:IconButton = controlButton.buttonRef;
			if(button == null)
			{
				button = new IconButton();
			}
			
			button.id = controlButton.sid;
			button.myId = controlButton.sid;
			button.commandByte = controlButton.commandByte;
			button.controlCommandBean = controlButton.controlCommandBean;
			button.top = controlButton.top;
			button.left = controlButton.left;
			button.buttonFeekbackStatus = controlButton.buttonFeekbackStatus;
			button.setStyle('icon',controlButton.upIcon);
			button.setStyle('icondown',controlButton.downIcon);
			button.setStyle('iconLabel',controlButton.buttonDisplayName);
			button.setStyle('skinClass', controlButton.skinClass);
			button.setStyle('sid', controlButton.sid);
			button.setStyle('rectstrokeAlpha', 0);
			if(controlButton.clickFunc != null)
			{
				button.addEventListener("click",controlButton.clickFunc);
			}
			if(controlButton.mouseDownFunc != null)
			{
				button.addEventListener("mouseDown",controlButton.mouseDownFunc);
			}
			if(controlButton.mouseUpFunc != null)
			{
				button.addEventListener("mouseUp",controlButton.mouseUpFunc);
			}
			view.addElement(button);
		}
		
		public static function updateLabelFontAndColor(label:Label, color:String, fontSize:int):void
		{
			label.setStyle('color',color);
			label.setStyle('fontSize',fontSize);	
		}
	}
	
	
}